﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;

namespace map.manage.Money
{
    public partial class WithDrawManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!chatblog.Util.ValidateAdmin ())
            {
                MessageBox.Show(this.Page ,"对不起，您没有权限访问此页面！");
                Response.Redirect(ResolveUrl ( "~/default.aspx"));
            }
            if (!IsPostBack)
            {
                InitPage();
                Bind1();
            }
        }

        #region 数据绑定
        private void Bind1()
        {
            if (chatblog.Util.ValidateAdmin())
            {
                string filter = "1=1 ";
                if (StartDateTextBox.Text != string.Empty)
                    filter += " and WithDrawDate>= '" + StartDateTextBox.Text + "'";
                if (EndDateTextBox.Text != string.Empty)
                    filter += " and WithDrawDate <= '" + EndDateTextBox.Text + " 23:59:59'";
                if (UserIdTextBox.Text != string.Empty)
                    filter += " and cbuserid = '" + UserIdTextBox.Text + "'";
                if (StatusDropDownList.SelectedValue != "")
                    filter += " and WithDrawStatus=" + StatusDropDownList.SelectedValue;

                cbWithDraw withDraw = cbWithDraw.Find(filter, " withdrawdate asc ,withdrawtime asc");
                this.WebPager1.DataSource = withDraw.MyTable;
                this.WebPager1.DataBind();
            }
        }

        private void InitPage()
        {
            DateTime now = DateTime.Now;
            this.StartDateTextBox.Text = now.Year.ToString() + "-" + now.Month.ToString() + "-1";
            this.EndDateTextBox.Text = now.ToShortDateString();
        }

        protected void WebPager1_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            WebPager1.CurrentPageIndex = e.NewPageIndex;
            Bind1();
        }
        #endregion

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            Bind1();
        }

        /// <summary>
        /// 返回提现状态的字符串
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        protected string GetWithDrawStatusStr(int status)
        {
            string result = string.Empty;
            if (status == RuleCode.Enum.cbWithDrawStatus.Applicated)
                result = "已申请";
            else if (status == RuleCode.Enum.cbWithDrawStatus.Approved)
                result = "已审核";
            else if (status == RuleCode.Enum.cbWithDrawStatus.Failed)      
                result = "提现失败";
            else if (status == RuleCode.Enum.cbWithDrawStatus.Finished)
                result = "提现成功";

            return result;
        }
        protected string GetAccountNumber(string accountNumber)
        {
            string result = string.Empty;
            if (accountNumber!=string.Empty )
            {
                result = accountNumber;
                if (result.Length > 7)
                {
                    string newResult = result;
                    result = string.Empty;
                    for (int i = 0; i < newResult.Length; i++)
                    {
                        if (i % 4 == 0 && i != 0)
                            result += " ";
                        if (i < 4 || i > newResult.Length - 4)
                            result += newResult[i].ToString();
                        else
                            result += "*";
                    }
                }
            }
            return result;
        }

        #region 按钮事件
        protected void SelectOthersCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            SelectAllCheckBox.Checked = false;
            for (int i = 0; i < DataList1.Items.Count; i++)
            {
                CheckBox checkBox = DataList1.Items[i].FindControl("CheckBox1") as CheckBox;
                checkBox.Checked = !checkBox.Checked;
            }

        }

        protected void SelectAllCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            SelectOthersCheckBox.Checked = false;
            for (int i = 0; i < DataList1.Items.Count; i++)
            {
                CheckBox checkBox = DataList1.Items[i].FindControl("CheckBox1") as CheckBox;
                checkBox.Checked = SelectAllCheckBox.Checked ;
            }
        }

        protected void ApprovedButton_Click(object sender, EventArgs e)
        {
            if (AliApi.appUserId != string.Empty)
            {
                for (int i = 0; i < DataList1.Items.Count; i++)
                {
                    CheckBox checkBox = DataList1.Items[i].FindControl("CheckBox1") as CheckBox;
                    if (checkBox.Checked)
                    {
                        string recid = DataList1.DataKeys[i].ToString();
                        cbWithDraw withDraw = cbWithDraw.Find("recid=" + recid);
                        if (withDraw.Count > 0 && withDraw.WithDrawStatus.Value == RuleCode.Enum.cbWithDrawStatus.Applicated)
                        {
                            withDraw.WithDrawStatus.Value = RuleCode.Enum.cbWithDrawStatus.Approved;
                            withDraw.ApprovedDate.Value = DateTime.Now;
                            withDraw.ApprovedBy.Value = AliApi.appUserId;
                            withDraw.Modify();
                        }
                    }
                }
                Bind1();
            }
            else
            {
                Response.Redirect(ResolveUrl ("~/default.aspx"));
            }
        }

        protected void FinishedButton_Click(object sender, EventArgs e)
        {
            if (AliApi.appUserId != string.Empty)
            {
                for (int i = 0; i < DataList1.Items.Count; i++)
                {
                    CheckBox checkBox = DataList1.Items[i].FindControl("CheckBox1") as CheckBox;
                    if (checkBox.Checked)
                    {
                        string recid =  DataList1.DataKeys[i].ToString();
                        cbWithDraw withDraw = cbWithDraw.Find("recid=" + recid);
                        if (withDraw.Count > 0 && withDraw.WithDrawStatus.Value == RuleCode.Enum.cbWithDrawStatus.Approved)
                        {
                            withDraw.WithDrawStatus.Value = RuleCode.Enum.cbWithDrawStatus.Finished;
                            withDraw.FinishedBy.Value = AliApi.appUserId;
                            withDraw.FinishedDate.Value = DateTime.Now;
                            withDraw.Modify();
                        }
                    }
                }
                Bind1();
            }
            else
            {
                Response.Redirect(ResolveUrl("~/default.aspx"));
            }
        }

        protected void FailedButton_Click(object sender, EventArgs e)
        {
            if (AliApi.appUserId != string.Empty)
            {
                for (int i = 0; i < DataList1.Items.Count; i++)
                {
                    CheckBox checkBox = DataList1.Items[i].FindControl("CheckBox1") as CheckBox;
                    if (checkBox.Checked)
                    {
                        string recid = DataList1.DataKeys[i].ToString();
                        cbWithDraw withDraw = cbWithDraw.Find("recid=" + recid);
                        if (withDraw.Count > 0 && withDraw.WithDrawStatus.Value != RuleCode.Enum.cbWithDrawStatus.Finished)
                        {
                            withDraw.WithDrawStatus.Value = RuleCode.Enum.cbWithDrawStatus.Failed;
                            withDraw.FinishedBy.Value = AliApi.appUserId;
                            withDraw.FinishedDate.Value = DateTime.Now;
                            withDraw.Modify();
                        }
                    }
                }
                Bind1();
            }
            else
            {
                Response.Redirect(ResolveUrl("~/default.aspx"));
            }
        }

        protected void PrintButton_Click(object sender, EventArgs e)
        {

        }
        #endregion

       

    }
}
